package com.bbx.taxi.client.Activity;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import butterknife.InjectView;

import com.bbx.androidapi.util.SharedPreUtil;
import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Activity.Menu.SetActivity;
import com.bbx.taxi.client.Activity.login.LoginActivity;
import com.bbx.taxi.client.Bean.Attribute.ChangeAttribute;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Bean.Extra.MainMsg;
import com.bbx.taxi.client.DB.TagMessageDB;
import com.bbx.taxi.client.Util.IsViewOutArea;
import com.bbx.taxi.client.widget.MaxByteLengthEditText;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickLeftListener;
import com.bbx.taxi.client.widget.Dailog.MyAlertDailog.onClickRightListener;
import com.bbx.taxi.client.widget.tagview.Tag;
import com.bbx.taxi.client.widget.tagview.TagListView;
import com.bbx.taxi.client.widget.tagview.TagListView.OnDelClickListener;
import com.bbx.taxi.client.widget.tagview.TagListView.OnTagCheckedChangedListener;
import com.bbx.taxi.client.widget.tagview.TagListView.OnTagClickListener;
import com.bbx.taxi.client.widget.tagview.TagListView.OnTagLongClickListener;
import com.bbx.taxi.client.widget.tagview.TagView;
import com.bbx.taxi.official.R;

public class LeaveMessageActivity extends BaseBBXActivity implements
		OnClickListener {
	@InjectView(R.id.et_message)
	MaxByteLengthEditText et_message;
	@InjectView(R.id.tagview)
	TagListView mTagListView;
	private MainAttribute main;
	private TagMessageDB db_tag;
	private List<String> list_reason =new ArrayList<String>();

	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_leave_message);
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void initView() {
		setTitle(true, getString(R.string.mian_message), -1,
				getString(R.string.titile_right_finish));
		et_message.setMaxByteLength(400);
		if (getIntent().getExtras() != null) {
			MainAttribute main = MainAttribute.getInstance();
			ChangeAttribute change = ChangeAttribute.getInstance();
			switch (getIntent().getExtras().getInt(MainMsg.extra_message)) {
			case MainMsg.MESSAGE_MAIN_GW:
				if (main.getMessage_GW() != null
						&& !main.getMessage_GW().equals("")) {
					et_message.setText(main.getMessage_GW());
					et_message.setSelection(et_message.length());
				}
				break;
			case MainMsg.MESSAGE_MAIN_PC:
				if (main.getMessage_PC() != null
						&& !main.getMessage_PC().equals("")) {
					et_message.setText(main.getMessage_PC());
					et_message.setSelection(et_message.length());
				}
				break;
			case MainMsg.MESSAGE_MAIN_BC:
				if (main.getMessage_BC() != null
						&& !main.getMessage_BC().equals("")) {
					et_message.setText(main.getMessage_BC());
					et_message.setSelection(et_message.length());
				}
				break;
			case MainMsg.MESSAGE_MAIN_KJ:
				if (main.getMessage_KJ() != null
						&& !main.getMessage_KJ().equals("")) {
					et_message.setText(main.getMessage_KJ());
					et_message.setSelection(et_message.length());
				}
				break;
			case MainMsg.MESSAGE_CHANGE_PC:
				if (change.getMessage_PC() != null
						&& !change.getMessage_PC().equals("")) {
					et_message.setText(change.getMessage_PC());
					et_message.setSelection(et_message.length());
				}
				break;
			case MainMsg.MESSAGE_CHANGE_BC:
				if (change.getMessage_BC() != null
						&& !change.getMessage_BC().equals("")) {
					et_message.setText(change.getMessage_BC());
					et_message.setSelection(et_message.length());
				}
				break;
			case MainMsg.MESSAGE_CHANGE_KJ:
				if (main.getMessage_KJ() != null
						&& !main.getMessage_KJ().equals("")) {
					et_message.setText(main.getMessage_KJ());
					et_message.setSelection(et_message.length());
				}
			}
		}
	}

	@Override
	protected void initData() {
		db_tag=new TagMessageDB(context);
		new AsyncTask<Void, Void, Void>(){

			@Override
			protected Void doInBackground(Void... arg0) {
				list_reason=db_tag.onRead(context);
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				initTag();
				super.onPostExecute(result);
			}
			
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		
	}

	public void initTag() {
		if(list_reason==null||list_reason.isEmpty()){
			list_reason.add("选择调研");
			list_reason.add("开会");
			list_reason.add("检查");
			list_reason.add("保密");
			list_reason.add("科研");
			list_reason.add("添加推荐事由+");
		}
		List<Tag> mTags = new ArrayList<Tag>();
		for (int i = 0; i < list_reason.size(); i++) {
			Tag tag = new Tag();
			tag.setId(i);
			tag.setChecked(false);
			tag.setTitle(list_reason.get(i));
			// 最后一项，不显示删除
			if (i == list_reason.size() - 1) {
				tag.setIsSpecial(true);
			}
			mTags.add(tag);
		}

		mTagListView.setTags(mTags, false);
		mTagListView.setOnTagLongClickListener(new OnTagLongClickListener() {

			@Override
			public void onTagLongClick(TagView tagView, Tag tag) {
				if (!tag.getIsSpecial()) {
				mTagListView.setDeleteMode(true);
				}
			}
		});
		mTagListView.setOnTagClickListener(new OnTagClickListener() {

			@Override
			public void onTagClick(TagView tagView, Tag tag) {
				if (tag.getIsSpecial()) {
					if(mTagListView.getDeleteMode()){
						mTagListView.setDeleteMode(false); 
					}
					if (mTagListView.getTags() != null
							&& mTagListView.getTags().size() >= 1) {
						final MyAlertDailog dialog = new MyAlertDailog(context);
						dialog.setEditText("添加事由标签");
						dialog.setLeftButtonText(getString(R.string.mydailog_cancel));
						dialog.setRightButtonText(getString(R.string.mydailog_confirm));
						if (dialog != null && !dialog.isShowing()) {
							dialog.show();
						}
						dialog.setOnClickRightListener(new onClickRightListener() {

							@Override
							public void onClickRight() {
								String text = dialog.getEditText();
								if(text.trim().equals("")){
									ToastUtil.showToast(context, "不能为空");
									return ;
									
								}
								for(int i=0;i< mTagListView.getTags().size();i++){
									if(mTagListView.getTags().get(i).getTitle().equals(text.trim())){
										ToastUtil.showToast(context, "已经有事由标签");
										return ;
									}
								}
								int index = mTagListView.getTags().size() - 1;
								Tag tag2 = new Tag();
								tag2.setId(index);
								tag2.setChecked(false);
								tag2.setTitle(text);
								mTagListView.addTag(tag2, index);
								// 设置特殊一项的id为最后
								Tag Special = mTagListView.getTags().get(
										mTagListView.getTags().size() - 1);
								Special.setId(mTagListView.getTags().size() - 1);
								//存储
								list_reason.add(index,text);
								db_tag.onWrite(list_reason);


							}
						});

					}

				}else{
					et_message.setText(et_message.getText().toString()+tag.getTitle());
				}

			}
		});
		mTagListView.setOnDelClickListener(new OnDelClickListener() {

			@Override
			public void onDelClick(TagView tagView, Tag tag, ImageView iv_del,
					RelativeLayout layout) {
				mTagListView.removeTag(tag, tagView, iv_del, layout);
				//存储
				list_reason.remove(tag.getTitle());
				db_tag.onWrite(list_reason);			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_right:
			String message = et_message.getText().toString().toString();
			Bundle bundle = new Bundle();
			bundle.putString("message", message);
			Intent intent = new Intent();
			intent.putExtras(bundle);
			setResult(resultCode, intent);
			onfinish();
			break;
		default:
			super.onClick(v);
			break;
		}
	}

	public void setEnabled(Button v, boolean enabled) {
		if (enabled) {
			v.setEnabled(enabled);
			v.setTextColor(getResources().getColor(R.color.text_nor));
		} else {
			v.setEnabled(enabled);
			v.setTextColor(getResources().getColor(R.color.white));
		}

	}
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (ev.getAction() == MotionEvent.ACTION_DOWN) {
			if (IsViewOutArea.isViewOutArea(mTagListView, ev)) {
				mTagListView.setDeleteMode(false); 
			}
			return super.dispatchTouchEvent(ev);
		}
		// 必不可少，否则所有的组件都不会有TouchEvent了
		if (getWindow().superDispatchTouchEvent(ev)) {
			return true;
		}
		return onTouchEvent(ev);
	}
}
